<?php
/**
 * Created by PhpStorm.
 * User: v.xiaqc
 * Date: 2019/7/3
 * Time: 10:52
 */
include 'pageInterface.php';

class Page implements PageInterface
{
    protected $page;
    protected $pageSize; //每页条数
    protected $totalCount; //总记录数
    protected $totalPage; //总页数
    protected $previous = '&laquo;';
    protected $next = '&raquo;';

    public function __construct($total, $pageSize = 15)
    {
        $this->totalCount = $total;
        $this->pageSize = $pageSize;
        //计算总共分几页
        $this->totalPage = ceil($total / $pageSize);
        $this->page = $this->getPage();
    }

    public function show()
    {
        // TODO: Implement show() method.

        if (empty($this->totalPage)) {
            return ;
        }
        //循环开始数
        $cell = ceil($this->page/8);

        $start = (($cell - 1) * 8) + 1;

        if ($this->page%8 == 0){
            $cell = $cell + 1;
            $start = $start + 6;
        }
        if ($this->page != 1 && ($this->page-1)%8 == 0){
            $start = $start - 1;
        }

        $end = ($cell * 8) > $this->totalPage ? $this->totalPage : ($cell * 8);

        if ($this->page%8 == 0){
            $end = $end - 2;
        }
        if ($this->page != 1 && ($this->page-1)%8 == 0){
            $end = $end - 1;
        }

        return $this->getUrlRange($start, $end);

    }

    /**
     * 总记录数
     * @param $total
     * @return mixed
     */
    public function count($total)
    {
        // TODO: Implement count() method.
        return $this->totalCount = $total;
    }

    /**
     * 起始页
     * @return float|int
     */
    public function firstPage()
    {
        // TODO: Implement firstPage() method.
        return ($this->page - 1) * $this->pageSize;
    }

    /**
     * 每页记录条数
     * @return int
     */
    public function lastPage()
    {
        // TODO: Implement lasePage() method.
        return $this->pageSize;
    }

    /**
     * 组装页码
     * @param $start
     * @param $end
     * @return string
     */
    public function getUrlRange($start, $end)
    {
        $prePage = $this->page - 1;
        $nextPage = $this->page + 1;
        $html = "<nav aria-label='Page navigation'>
                 <ul class='pagination'>";

        if ($this->page == 1) {
            $html.="<li class='disabled'>
                      <a href='#' aria-label='Previous'>
                        <span aria-hidden='true'>{$this->previous}</span>
                      </a>
                    </li>";
        }else {
            $html.="<li>
                      <a href='{$this->url($prePage)}' aria-label='Previous'>
                        <span aria-hidden='true'>{$this->previous}</span>
                      </a>
                    </li>";
        }

        for ($start; $start <= $end; $start++){
            if ($this->page == $start) {
                $html.="<li class='active'><a href='#'>{$start} <span class='sr-only'>(current)</span></a></li>";
            }else {
                $html .= "<li><a href='{$this->url($start)}'>{$start}</a></li>";
            }
        }
        if ($this->page == $this->totalPage){
            $html.="<li class='disabled'>
                      <a href='#' aria-label='Previous'>
                        <span aria-hidden='true'>{$this->next}</span>
                      </a>
                    </li>";
        }else {
            $html .= "
                    <li>
                      <a href='{$this->url($nextPage)}' aria-label='Next'>
                        <span aria-hidden='true'>{$this->next}</span>
                      </a>
                    </li>";
        }

        $html.="</ul></nav>";
        return $html;
    }

    /**
     * 获取url
     */
    public function url($page)
    {
        return $_SERVER['DOCUMENT_URI'] . '?p=' . $page;
    }

    /**
     * 获取页码
     * @return float|int
     */
    public function getPage()
    {
        if (intval($_GET['p']) > $this->totalPage) {
            return $this->totalPage;
        }
        return intval($_GET['p']) <= 0 ? 1 : intval($_GET['p']);
    }

}